package com.cheer.ba.view.activity;

import android.os.Handler;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cheer.ba.R;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.presenter.impl.LoginPresenter;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.view.base.BaseCommAty;
import com.litesuits.common.utils.HexUtil;
import com.litesuits.common.utils.MD5Util;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 修改密码
 *
 * @author HQ
 *         created at 2017/4/1 0001 15:42
 */
public class ModifyPwAty extends BaseCommAty {

    //旧密码
    @BindView(R.id.id_input_old_pw_edit)
    EditText mEditOldPw;
    private String mOldPw;

    @BindView(R.id.id_clear_old_pw)
    ImageView mClearOldPw;

    //新密码
    @BindView(R.id.id_input_new_pw_edit)
    EditText mEditNewPw;
    private String mNewPw;

    @BindView(R.id.id_clear_new_pw)
    ImageView mClearNewPw;
    @BindView(R.id.id_watch_new_pass)
    ImageView mWatchNewPw;

    //再次输入新密码
    @BindView(R.id.id_input_re_new_pw_edit)
    EditText mEditReNewPw;
    private String mReNewPw;

    @BindView(R.id.id_clear_re_new_pw)
    ImageView mClearReNewPw;
    @BindView(R.id.id_watch_re_new_pass)
    ImageView mWatchReNewPw;


    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_modify_pw, null, false));
    }

    @Override
    public boolean hasDataInPage() {
        return true;
    }

    @Override
    public void initPresenter() {
        presenter = new LoginPresenter();
    }

    @Override
    public void initView() {
        setTitleText(getString(R.string.modify_pw));
        mEditOldPw.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int arg1, int arg2,
                                      int arg3) {
                mOldPw = s + "";
                if (!StringUtils.isEmpty(mOldPw)) {
                    mClearOldPw.setVisibility(View.VISIBLE);
                } else {
                    mClearOldPw.setVisibility(View.GONE);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {
            }

            @Override
            public void afterTextChanged(Editable arg0) {
            }
        });
        mEditOldPw.setKeyListener(new DigitsKeyListener() {
            @Override
            public int getInputType() {
                return InputType.TYPE_TEXT_VARIATION_PASSWORD;
            }

            @Override
            protected char[] getAcceptedChars() {
                char[] data = getResources().getString(
                        R.string.edit_only_can_input).toCharArray();
                return data;
            }
        });

        mEditNewPw.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int arg1, int arg2,
                                      int arg3) {
                mNewPw = s + "";
                if (!StringUtils.isEmpty(mNewPw)) {
                    mClearNewPw.setVisibility(View.VISIBLE);
                } else {
                    mClearNewPw.setVisibility(View.GONE);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {
            }

            @Override
            public void afterTextChanged(Editable arg0) {
            }
        });
        mEditNewPw.setKeyListener(new DigitsKeyListener() {
            @Override
            public int getInputType() {
                return InputType.TYPE_TEXT_VARIATION_PASSWORD;
            }

            @Override
            protected char[] getAcceptedChars() {
                char[] data = getResources().getString(
                        R.string.edit_only_can_input).toCharArray();
                return data;
            }
        });

        mEditReNewPw.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int arg1, int arg2,
                                      int arg3) {
                mReNewPw = s + "";
                if (!StringUtils.isEmpty(mReNewPw)) {
                    mClearReNewPw.setVisibility(View.VISIBLE);
                } else {
                    mClearReNewPw.setVisibility(View.GONE);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {
            }

            @Override
            public void afterTextChanged(Editable arg0) {
            }
        });
        mEditReNewPw.setKeyListener(new DigitsKeyListener() {
            @Override
            public int getInputType() {
                return InputType.TYPE_TEXT_VARIATION_PASSWORD;
            }

            @Override
            protected char[] getAcceptedChars() {
                char[] data = getResources().getString(
                        R.string.edit_only_can_input).toCharArray();
                return data;
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
    }

    /**
     * 校验
     */
    public boolean checkData() {
        if (StringUtils.isEmpty(mOldPw) || mOldPw.length() > 18 || mOldPw.length() < 6 ||
                StringUtils.isEmpty(mNewPw) || mNewPw.length() > 18 || mNewPw.length() < 6 ||
                StringUtils.isEmpty(mReNewPw) || mReNewPw.length() > 18 || mReNewPw.length() < 6) {
            SVProgressHUD.showInfoWithStatus(aty, getString(R.string.input_new_pw));
        } else {
            if (StringUtils.isNumRic(mOldPw) || StringUtils.isLetterRic(mOldPw) ||
                    StringUtils.isNumRic(mNewPw) || StringUtils.isLetterRic(mNewPw) ||
                    StringUtils.isNumRic(mReNewPw) || StringUtils.isLetterRic(mReNewPw)) {
                SVProgressHUD.showInfoWithStatus(aty, getString(R.string.pw_had_num_letter));
            } else {
                if (!TextUtils.equals(mNewPw, mReNewPw)) {
                    SVProgressHUD.showInfoWithStatus(aty, getString(R.string.input_no_right));
                } else {
                    return true;
                }
            }
        }
        return false;
    }

    @OnClick({R.id.id_clear_old_pw, R.id.id_clear_new_pw, R.id.id_watch_new_pass, R.id.id_clear_re_new_pw, R.id.id_watch_re_new_pass, R.id.id_modify_sure_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            //清除旧密码
            case R.id.id_clear_old_pw:
                mEditOldPw.setText("");
                break;
            //清除新密码
            case R.id.id_clear_new_pw:
                mEditNewPw.setText("");
                break;
            case R.id.id_watch_new_pass:
                int length = TextUtils.isEmpty(mEditNewPw.getText()) ? 0 : mEditNewPw.getText().length();
                if (mEditNewPw.getTransformationMethod() == PasswordTransformationMethod.getInstance()) {
                    mEditNewPw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mWatchNewPw.setImageDrawable(getResources().getDrawable(R.drawable.ic_eye_on));
                } else {
                    mEditNewPw.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mWatchNewPw.setImageDrawable(getResources().getDrawable(R.drawable.ic_eye_off));
                }
                mEditNewPw.setSelection(length);
                break;
            //清除第二次输入的新密码
            case R.id.id_clear_re_new_pw:
                mEditReNewPw.setText("");
                break;
            case R.id.id_watch_re_new_pass:
                int length1 = TextUtils.isEmpty(mEditReNewPw.getText()) ? 0 : mEditReNewPw.getText().length();
                if (mEditReNewPw.getTransformationMethod() == PasswordTransformationMethod.getInstance()) {
                    mEditReNewPw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mWatchReNewPw.setImageDrawable(getResources().getDrawable(R.drawable.ic_eye_on));
                } else {
                    mEditReNewPw.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mWatchReNewPw.setImageDrawable(getResources().getDrawable(R.drawable.ic_eye_off));
                }
                mEditReNewPw.setSelection(length1);
                break;
            //修改密碼
            case R.id.id_modify_sure_btn:
                if (com.cheer.ba.utils.CountDownTimer.isFastClick()) {
                    return;
                }
                if (checkData()) {
                    ((LoginPresenter) presenter).modifyLoginPw(HexUtil.encodeHexStr(MD5Util.md5(mOldPw)), HexUtil.encodeHexStr(MD5Util.md5(mNewPw)));
                }
                break;
        }
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (TextUtils.equals(ConstantTag.MODIFY_LOGIN_PW, tag)) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    setResult(IntentCode.Forget.MODIFY_PW_RESULT_CODE);
                    finish();
                }
            }, 2000);
        }
    }
}
